<template>
  <div class="top-category">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread separator="/">
        <!-- 123
        <p>456</p>
        <p>789</p> -->
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem>{{ cateName }}</XtxBreadItem>
      </XtxBread>

      <!-- 轮播图 -->
      <xtx-slider :data="slidersData" />

      <!-- 全部分类（二级分类） -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="second in categoryList" :key="second.id">
            <a href="javascript:;">
              <img :src="second.picture" />
              <p>{{ second.name }}</p>
            </a>
          </li>
        </ul>
      </div>

      <!-- 分类关联商品 -->
      <div class="ref-goods" v-for="second in categoryList" :key="second.id">
        <!-- 二级分类名字 -->
        <div class="head">
          <h3>- {{ second.name }} -</h3>
          <XtxMore to="/" />
        </div>
        <div class="body">
          <!-- 二级分类对应的商品列表
          作业：封装单个商品渲染显示的组件=》prop:good(单个商品数据)
           -->
          <GoodsItem
            v-for="good in second.goods"
            :key="good.id"
            :goods="good"
          />
          <!-- <RouterLink
            to="/"
            class="goods-item"
            v-for="good in second.goods"
            :key="good.id"
          >
            <img :src="good.picture" alt="" />
            <p class="name ellipsis">{{ good.name }}</p>
            <p class="desc ellipsis">{{ good.desc }}</p>
            <p class="price">&yen;{{ good.price }}</p>
          </RouterLink> -->
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { useBanner } from '@/hooks/page'
import { findAllCategory } from '@/api/category'
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import GoodsItem from './components/goods-item.vue'
// 获取所有分类
const useCategory = () => {
  // route === this.$route =>vue3获取路由参数
  const route = useRoute()
  const categoryList = ref([])
  // 分类名
  const cateName = ref('')
  const getCategroyList = async () => {
    const { result } = await findAllCategory(route.params.id)
    console.log('分类：', result)
    categoryList.value = result.children
    cateName.value = result.name
  }
  onMounted(() => {
    console.log('组件被创建了~')
    getCategroyList()
  })
  return { categoryList, cateName }
}
export default {
  name: 'TopCategory',
  components: {
    GoodsItem
  },
  setup () {
    const { slidersData } = useBanner()
    const { categoryList, cateName } = useCategory()

    return { slidersData, categoryList, cateName }
  },
  created () {
    // vue2获取路由参数
    console.log(this.$route.params)
  }
}
</script>

<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: space-around;
      padding: 0 40px 30px;
    }
  }
}
</style>
